package com.ruoyi.project.system.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.project.system.domain.SysUserProductPrice;
import com.ruoyi.project.system.mapper.SysUserProductPriceMapper;
import com.ruoyi.project.system.service.ISysUserProductPriceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 用户产品定价Service业务层处理
 *
 * @author ruoyi
 * @date 2024-07-23
 */
@Service
public class SysUserProductPriceServiceImpl implements ISysUserProductPriceService {

    @Autowired
    private SysUserProductPriceMapper sysUserProductPriceMapper;

    /**
     * 查询用户产品定价列表
     *
     * @param sysUserProductPrice 用户产品定价
     * @return 用户产品定价
     */
    @Override
    public List<SysUserProductPrice> selectSysUserProductPriceList(SysUserProductPrice sysUserProductPrice) {
        return sysUserProductPriceMapper.selectSysUserProductPriceList(sysUserProductPrice);
    }

    /**
     * 新增用户产品定价
     *
     * @param sysUserProductPrice 用户产品定价
     * @return 结果
     */
    @Override
    public int insertSysUserProductPrice(SysUserProductPrice sysUserProductPrice) {
        sysUserProductPrice.setCreateTime(DateUtils.getNowDate());
        return sysUserProductPriceMapper.insertSysUserProductPrice(sysUserProductPrice);
    }

    /**
     * 修改用户产品定价
     *
     * @param sysUserProductPrice 用户产品定价
     * @return 结果
     */
    @Override
    public int updateSysUserProductPrice(SysUserProductPrice sysUserProductPrice) {
        sysUserProductPrice.setUpdateTime(DateUtils.getNowDate());
        return sysUserProductPriceMapper.updateSysUserProductPrice(sysUserProductPrice);
    }

    /**
     * 批量删除用户产品定价
     *
     * @param ids 需要删除的用户产品定价主键
     * @return 结果
     */
    @Override
    public int deleteSysUserProductPriceByIds(Long[] ids) {
        return sysUserProductPriceMapper.deleteSysUserProductPriceByIds(ids);
    }

    @Override
    public int setPrice(SysUserProductPrice sysUPP) {
        SysUserProductPrice sysUserProductPrice = sysUserProductPriceMapper.selectSysUserProductPrice(sysUPP.getUserId(), sysUPP.getProductId());
        if (sysUserProductPrice == null) { // 直接没有价格记录，执行插入
            return sysUserProductPriceMapper.insertSysUserProductPrice(sysUPP);
        }
        // 有价格记录 执行修改
        return sysUserProductPriceMapper.updatePriceByUserIdAndProductId(sysUPP);
    }
}

